package com.easyjava.builder;

import com.easyjava.bean.Constants;
import com.easyjava.bean.FieldInfo;
import com.easyjava.bean.TableInfo;
import com.easyjava.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedWriter;
import java.io.File;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BuildServiceImpl
 * @Date 2024/8/8  15:25
 **/
public class BuildController {
    private static final Logger logger = LoggerFactory.getLogger(BuildService.class);

    public static void execute(TableInfo tableInfo) {


        File folder = new File(Constants.PATH_CONTROLLER);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        String className = tableInfo.getBeanName() + "Controller";
        File file = new File(folder, className + ".java");


        try {
            OutputStream outputStream = Files.newOutputStream(file.toPath());
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "utf8");
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            bufferedWriter.write("package " + Constants.PACKAGE_CONTROLLER+ ";");
            bufferedWriter.newLine();

            bufferedWriter.write("import java.util.List;");
            bufferedWriter.newLine();
            String serviceName = tableInfo.getBeanName() + Constants.SUFFIX_SERVICE;
            String serviceBeanName = StringUtils.lowerCaseFirstLetter(serviceName);
            bufferedWriter.write("import " + Constants.PACKAGE_PO + "."+ tableInfo.getBeanName()+";");
            bufferedWriter.newLine();
            bufferedWriter.write("import " + Constants.PACKAGE_QUERY + "." +tableInfo.getBeanParamName() +";");
            bufferedWriter.newLine();
            bufferedWriter.write("import " + Constants.PACKAGE_VO + ".PaginationResultVO;");
            bufferedWriter.newLine();
            bufferedWriter.write("import " + Constants.PACKAGE_SERVICE + "." + serviceName + ";");
            bufferedWriter.newLine();
            bufferedWriter.write("import " + Constants.PACKAGE_ENUMS + ".PageSize;");
            bufferedWriter.newLine();
            bufferedWriter.write("import " + Constants.PACKAGE_QUERY + ".SimplePage;");
            bufferedWriter.newLine();
            bufferedWriter.write("import javax.annotation.Resource;");
            bufferedWriter.newLine();
            bufferedWriter.write("import org.springframework.web.bind.annotation.RestController;");
            bufferedWriter.newLine();
            bufferedWriter.write("import org.springframework.web.bind.annotation.RequestMapping;");
            bufferedWriter.newLine();
            BuildComment.createClassComment(bufferedWriter, tableInfo.getComment() + "Service");
            bufferedWriter.write("@RestController");
            bufferedWriter.newLine();
            bufferedWriter.write("@RequestMapping(\""+StringUtils.lowerCaseFirstLetter(tableInfo.getBeanName())+"\")");
            bufferedWriter.newLine();
            bufferedWriter.write("public class " + className + " extends ABaseController{");
            bufferedWriter.newLine();
            bufferedWriter.write("\t@Resource");
            bufferedWriter.newLine();
            bufferedWriter.write("\tprivate " + serviceName + " " + serviceBeanName + ";");
            bufferedWriter.newLine();
            BuildComment.createFieldComment(bufferedWriter,"根据条件分页查询");
            bufferedWriter.write("\t@RequestMapping(\"loadDataList\")");
            bufferedWriter.newLine();
            bufferedWriter.write("\tpublic ResponseVO loadDataList(" + tableInfo.getBeanName() + Constants.SUFFIX_BEAN_QUERY + " query){");
            bufferedWriter.newLine();
            bufferedWriter.write("\t\treturn getSuccessResponseVO(" + serviceBeanName + ".findListByPage(query));");
            bufferedWriter.newLine();
            bufferedWriter.write("\t}");
            bufferedWriter.newLine();
            bufferedWriter.newLine();

            BuildComment.createFieldComment(bufferedWriter,"新增");
            bufferedWriter.write("\t@RequestMapping(\"add\")");
            bufferedWriter.newLine();
            bufferedWriter.write("\tpublic ResponseVO add("+tableInfo.getBeanName()+" bean){");
            bufferedWriter.newLine();
            bufferedWriter.write("\t\t" + serviceBeanName + ".add(bean);");
            bufferedWriter.newLine();
            bufferedWriter.write("\t\treturn getSuccessResponseVO(null);");
            bufferedWriter.newLine();
            bufferedWriter.write("\t}");
            bufferedWriter.newLine();
            bufferedWriter.newLine();


            BuildComment.createFieldComment(bufferedWriter,"批量新增");
            bufferedWriter.write("\t@RequestMapping(\"addBatch\")");
            bufferedWriter.newLine();
            bufferedWriter.write("\tpublic ResponseVO addBatch(@RequestBody List<"+tableInfo.getBeanName()+"> listBean){");
            bufferedWriter.newLine();
            bufferedWriter.write("\t\t" + serviceBeanName + ".addBatch(listBean);");
            bufferedWriter.newLine();
            bufferedWriter.write("\t\treturn getSuccessResponseVO(null);");
            bufferedWriter.newLine();
            bufferedWriter.write("\t}");
            bufferedWriter.newLine();
            bufferedWriter.newLine();


            BuildComment.createFieldComment(bufferedWriter,"批量新增或修改");
            bufferedWriter.write("\t@RequestMapping(\"addOrUpdateBatch\")");
            bufferedWriter.newLine();
            bufferedWriter.write("\tpublic ResponseVO addOrUpdateBatch(List<"+tableInfo.getBeanName()+"> listBean){");
            bufferedWriter.newLine();
            bufferedWriter.write("\t\t" + serviceBeanName + ".addOrUpdateBatch(listBean);");
            bufferedWriter.newLine();
            bufferedWriter.write("\t\treturn getSuccessResponseVO(null);");
            bufferedWriter.newLine();
            bufferedWriter.write("\t}");
            bufferedWriter.newLine();
            bufferedWriter.newLine();


            for (Map.Entry<String, List<FieldInfo>> entry :  tableInfo.getKeyIndexMap().entrySet()) {
                List<FieldInfo> keyFieldInfoList = entry.getValue();
                int index = 0;
                StringBuilder methodName = new StringBuilder();
                StringBuilder methodParamsWithType = new StringBuilder();
                StringBuilder methodParams = new StringBuilder();
                for (FieldInfo fieldInfo : keyFieldInfoList) {
                    index++;
                    methodName.append(StringUtils.uperCaseFirstLetter(fieldInfo.getPropertyName()));
                    if (index < keyFieldInfoList.size()) {
                        methodName.append("And");
                    }
                    methodParamsWithType.append(fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName());
                    methodParams.append(fieldInfo.getPropertyName());
                    if (index < keyFieldInfoList.size()) {
                        methodParamsWithType.append(",");
                        methodParams.append(",");
                    }
                }
                BuildComment.createFieldComment(bufferedWriter,"根据"+ methodName + "查询");
                bufferedWriter.write("\t@RequestMapping(\"get" + tableInfo.getBeanName()+ "By" + methodName +"\")");
                bufferedWriter.newLine();
                bufferedWriter.write("\tpublic ResponseVO get"+ tableInfo.getBeanName()+ "By" + methodName + "("+methodParamsWithType+"){");
                bufferedWriter.newLine();
                bufferedWriter.write("\t\treturn getSuccessResponseVO(" + serviceBeanName + ".get"+ tableInfo.getBeanName()+ "By"+ methodName + "(" + methodParams + "));");
                bufferedWriter.newLine();
                bufferedWriter.write("\t}");
                bufferedWriter.newLine();
                bufferedWriter.newLine();

                BuildComment.createFieldComment(bufferedWriter,"根据"+ methodName + "更新");
                bufferedWriter.write("\t@RequestMapping(\"update" + tableInfo.getBeanName() + "By" + methodName +"\")");
                bufferedWriter.newLine();
                bufferedWriter.write("\tpublic ResponseVO update"+ tableInfo.getBeanName() + "By" + methodName + "("+tableInfo.getBeanName() + " bean, " + methodParamsWithType + "){");
                bufferedWriter.newLine();
                bufferedWriter.write("\t\t" + serviceBeanName + ".update"+ tableInfo.getBeanName()+ "By"+ methodName + "(bean," + methodParams + ");");
                bufferedWriter.newLine();
                bufferedWriter.write("\t\treturn getSuccessResponseVO(null);");
                bufferedWriter.newLine();
                bufferedWriter.write("\t}");
                bufferedWriter.newLine();
                bufferedWriter.newLine();

                BuildComment.createFieldComment(bufferedWriter,"根据"+ methodName + "删除");
                bufferedWriter.write("\t@RequestMapping(\"delete"+ tableInfo.getBeanName() + "By"+ methodName +"\")");
                bufferedWriter.newLine();
                bufferedWriter.write("\tpublic ResponseVO delete"+ tableInfo.getBeanName() + "By" + methodName + "(" + methodParamsWithType + "){");
                bufferedWriter.newLine();
                bufferedWriter.write("\t\t" + serviceBeanName + ".delete"+ tableInfo.getBeanName()+ "By"+ methodName + "(" + methodParams + ");");
                bufferedWriter.newLine();
                bufferedWriter.write("\t\treturn getSuccessResponseVO(null);");
                bufferedWriter.newLine();
                bufferedWriter.write("\t}");
                bufferedWriter.newLine();
                bufferedWriter.newLine();
            }

            bufferedWriter.write("}");
            bufferedWriter.flush();
        } catch (Exception e) {
            logger.error("创建serviceIml失败", e);
        }
    }
}
